package com.ceshi.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ceshi.dao.ProductDao;
import com.ceshi.dao.TransDao;
import com.ceshi.meta.Product;
import com.ceshi.meta.Trans;
import com.ceshi.meta.buyList;

@Controller
public class TransAction {

	@RequestMapping(value = "/settleAccount", method = RequestMethod.GET)
	public String settleAccount(HttpServletRequest req, ModelMap map) {
		System.out.println("settleAccount:");
		return "settleAccount";
	}

	@RequestMapping(value = "/account", method = RequestMethod.GET)
	public String account(HttpServletRequest req, ModelMap map) {
		// start session
		HttpSession session = req.getSession();
		String usertype = "0";
		int userid = 0;

		if (session.getAttribute("username") != null) {
			usertype = (String) session.getAttribute("usertype");
			userid = (Integer) session.getAttribute("userid");
		}

		ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
		TransDao transDao = context.getBean(TransDao.class);
		List<Trans> trans = transDao.getTransbyUserId(userid);
		List<Map> buyList = new ArrayList<Map>();
		
		for (Trans tran : trans) {
			Map haha1 = new HashMap();
			
			haha1.put("id", tran.getContentId());

			ProductDao productDao = context.getBean(ProductDao.class);
			Product product = productDao.getProductById(tran.getContentId());
			
			haha1.put("title", product.getTitle());
			haha1.put("image", product.getIcon());
			haha1.put("buyPrice", tran.getPrice());
			haha1.put("buyNum", tran.getBuyNum());
			haha1.put("buyTime", tran.getTime());

			buyList.add(haha1);

			System.out.println("account:" + haha1);
		}

		map.addAttribute("buyList", buyList);
		return "account";
	}

	@RequestMapping(value = "/api/buy", method = RequestMethod.POST)
	public String buyapi(@RequestBody List<buyList> buylist, HttpServletRequest req, ModelMap map) {
		// start session
		HttpSession session = req.getSession();
		String usertype = "0";
		int userid = 0;

		if (session.getAttribute("username") != null) {
			usertype = (String) session.getAttribute("usertype");
			userid = (Integer) session.getAttribute("userid");
		}

		for (buyList buy : buylist) {
			System.out.println("buylist:" + buy.getId() + "|" + buy.getNumber());
			ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
			TransDao transDao = context.getBean(TransDao.class);
			Trans tran = new Trans();
			tran.setContentId(buy.getId());
			tran.setPersonId(userid);
			
			Date dt=new Date();
			tran.setTime(dt.getTime());
			
			ProductDao productDao = context.getBean(ProductDao.class);
			Product product = productDao.getProductById(buy.getId());
			tran.setPrice(product.getPrice());
			
			tran.setBuyNum(buy.getNumber());
			transDao.insertTrans(tran);
		}

		map.addAttribute("code", "200");
		map.addAttribute("message", "购买成功");
		map.addAttribute("result", true);
		return ("defaultViews");

	}

	@RequestMapping(value = "/api/delete", method = RequestMethod.POST)
	public String buyapi(@RequestParam int id, HttpServletRequest req, ModelMap map) {
		// start session
		HttpSession session = req.getSession();
		String usertype = "0";
		int userid = 0;

		if (session.getAttribute("username") != null) {
			usertype = (String) session.getAttribute("usertype");
			userid = (Integer) session.getAttribute("userid");
		}
		if (!usertype.equals("1")) {
			map.addAttribute("code", "400");
			map.addAttribute("message", "请先登录");
			map.addAttribute("result", false);
			return "defaultViews";
		}

		ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
		ProductDao productDao = context.getBean(ProductDao.class);
		productDao.deleteProduct(id);
		map.addAttribute("code", "200");
		map.addAttribute("message", "删除成功");
		map.addAttribute("result", true);
		return "defaultViews";
	}
}
